package com.dameng.system.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SystemUser extends Model<SystemUser> implements UserDetails {
    @TableId(type = IdType.ASSIGN_UUID)
    private String userId;
    private String username;
    private String name;
    private String password;
    private Integer type;
    private String deptId;
    private Integer degree;
    private Boolean status;// 启动1，禁用0
    private String mobile;
    private String email;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date pwdModifyTime;
    @TableField(exist = false)
    private String deptName;
    @TableField(exist = false)
    private SystemDept systemDept;
    @TableField(exist = false)
    private List<SystemRole> systemRoles;

    @Override
    protected Serializable pkVal() {
        return this.userId;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        if (systemRoles != null) {
            for (SystemRole role : systemRoles) {
                authorities.add(new SimpleGrantedAuthority(role.getName()));
            }
        }
        return authorities;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean isEnabled() {
        return status;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

}
